Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

deFunctors.hpp

Go to the documentation of this file.
00001 ///////////////////////////////////////////////////////////////////////////////
00002 /// @file deFunctors.hpp
00003 ///
00004 /// @brief templated functor classes
00005 ///
00006 /// @author Assassin
00007 ///
00008 /// This file is the intellectual property of Novus Delta, LLC.. Usage of the
00009 /// contents of this file is subject to the Destiny3D Member License which
00010 /// can be found at http://www.destiny3d.com.  Any other usage is prohibited.
00011 ///
00012 /// This file is distributed "AS IS" without warranty of any kind.  Novus
00013 /// Delta, LLC. does not guarantee the fitness of the contents of this file
00014 /// for any particular purpose.
00015 ///
00016 /// Copyright (C) 2001-2003 Novus Delta, LLC. All Rights Reserved.
00017 ///
00018 /// <hr>
00019 ///                                 Change History
00020 /// <hr>
00021 ///
00022 /// @date Dec 2002
00023 /// @author Assassin
00024 /// @remarks Creation
00025 ///
00026 ///////////////////////////////////////////////////////////////////////////////
00027 
00028 #ifndef DEFUNCTORS_HPP
00029 #define DEFUNCTORS_HPP
00030 
00031 #include "deGlobalTypes.hpp"
00032 
00033 typedef class IdeWorldObject IdeWorldObject;
00034 
00035 template <typename T>
00036 void deSwap(T & a, T& b)
00037 {
00038     T t = a;
00039     a = b;
00040     b = t;
00041 }
00042 
00043 template <class T>
00044 class deDelPtrsFunctor
00045 {
00046 public:
00047     inline void operator() (T* &ptr) const
00048     {
00049         delete ptr;
00050         ptr = NULL;
00051     }
00052 };
00053 
00054 template <class T>
00055 class deReleasePtrsFunctor
00056 {
00057 public:
00058     inline void operator() (T* &ptr) const
00059     {
00060         if (ptr)
00061             ptr->Release();
00062         ptr = NULL;
00063     }
00064 };
00065 
00066 template <class T>
00067 class deBoundsCheckedArray
00068 {
00069     T* m_array;
00070     int m_length;
00071 public:
00072     deBoundsCheckedArray() {m_array = 0; m_length = 0; }
00073     ~deBoundsCheckedArray() {}
00074     void operator=(T* refarray) { m_array = refarray;}
00075     void SetLength(int size) {m_length = size;}
00076     int  GetLength() {return m_length;}
00077     inline T& operator[](int index)
00078     {
00079         DE_ASSERT(m_array && index >= 0 && index < m_length);
00080         return m_array[index];
00081     }
00082 };
00083 
00084 // when using this class, you still have to release the pointer when you're all done
00085 // it just helps when you are passing pointers around between functions
00086 template <class T=IdeWorldObject>
00087 class deWorldObjectSmartPtr
00088 {
00089     T* m_ptr;
00090 public:
00091     explicit deWorldObjectSmartPtr() { m_ptr = 0; }
00092     explicit deWorldObjectSmartPtr(const deWorldObjectSmartPtr<T>&ref)
00093         { m_ptr = ref.m_ptr; if (m_ptr) m_ptr->Claim(); }
00094     ~deWorldObjectSmartPtr() { if (m_ptr) m_ptr->Release(); }
00095     T* operator=(T* new_ptr)
00096     {
00097         if (new_ptr) new_ptr->Claim();
00098         if (m_ptr) m_ptr->Release();
00099         m_ptr = new_ptr;
00100         return m_ptr;
00101     }
00102     T* operator ->() { return m_ptr; }
00103     T** operator &() { return &m_ptr; }
00104     operator T* () { return m_ptr; }
00105 };
00106 
00107 class HashEdge // for use in a deTHashFunctor object
00108 {
00109     DWORD m_Value;
00110 public:
00111     inline HashEdge(u16 index1, u16 index2)
00112     {
00113         if (index1 < index2)
00114             m_Value = index1 | (index2<<16);
00115         else
00116             m_Value = index2 | (index1<<16);
00117     }
00118     inline HashEdge(const HashEdge & ref) : m_Value(ref.m_Value) { }
00119     inline deBoolean operator==(const HashEdge & rhs)
00120     {
00121         return (m_Value == rhs.m_Value);
00122     }
00123     inline DWORD Hash() const
00124     {
00125         return m_Value;
00126     }
00127     inline u16 index1() const
00128     {
00129         return (u16)((m_Value >> 16) & 0x0000ffff);
00130     }
00131     inline u16 index2() const
00132     {
00133         return (u16)(m_Value & 0x0000ffff);
00134     }
00135 };
00136 
00137 class HashEdge32 // for use in a deTHashFunctor object
00138 {
00139     long m_Index1;
00140     long m_Index2;
00141 public:
00142     inline HashEdge32(long index1, long index2)
00143     {
00144         if (index1 < index2)
00145         {
00146             m_Index1 = index1;
00147             m_Index2 = index2;
00148         }
00149         else
00150         {
00151             m_Index1 = index2;
00152             m_Index2 = index1;
00153         }
00154     }
00155     inline HashEdge32(const HashEdge32 & ref) : m_Index1(ref.m_Index1), m_Index2(ref.m_Index2) { }
00156     inline deBoolean operator==(const HashEdge32 & rhs)
00157     {
00158         return (m_Index1 == rhs.m_Index1 && m_Index2 == rhs.m_Index2);
00159     }
00160     inline DWORD Hash() const
00161     {
00162         return ((m_Index1 << 16) | (m_Index1 >> 16)) ^ m_Index2;
00163     }
00164     inline long index1() const
00165     {
00166         return m_Index1;
00167     }
00168     inline long index2() const
00169     {
00170         return m_Index2;
00171     }
00172 };
00173 
00174 
00175 template <typename Y, class Eval, typename T>
00176 class deLazyEval
00177 {
00178 private:
00179     T m_ID;
00180     Y* m_Eval;
00181 public:
00182     deLazyEval(T id) : m_ID(id), m_Eval(0) {}
00183     ~deLazyEval(){} 
00184 
00185     operator Y* ()
00186     {   if (!m_Eval)
00187             m_Eval = Eval(m_ID);
00188         return m_Eval;
00189     }
00190 };
00191 
00192 /// iterates over an iterator range, executing func on each element unconditionally
00193 template <typename IT, class F>
00194 void deIter_App(IT first, const IT& last, F & func)
00195 {
00196     for (; first != last; ++first)
00197         func(*first);
00198 }
00199 /// iterates over an iterator range, executing func on each element, until func(e) evaluates to false
00200 /// @return deTRUE if all func(e) returned true for all elements, deFALSE otherwise
00201 template <typename IT, class F>
00202 deBoolean deIter_All(IT first, const IT& last, F & func)
00203 {
00204     for (; first != last; ++first)
00205     {
00206         if (!func(*first))
00207             return deFALSE;
00208     }
00209     return deTRUE;
00210 }
00211 /// iterates over an iterator range, until it finds the matching value, and erases it from container
00212 template <typename IT, class T, class C>
00213 void deFindErase(IT first, const IT& last, const T& val, C & container)
00214 {
00215     for (; first != last; ++first)
00216     {
00217         if (*first == val)
00218         {
00219             container.erase(first);
00220             return;
00221         }
00222     }
00223 }
00224 /// iterates over an iterator range, until it finds the matching value, and returns that iterator,
00225 /// or returns last if it was not found
00226 template <typename IT, class T>
00227 IT deFind(IT first, const IT& last, const T& val)
00228 {
00229     for (; first != last; ++first)
00230     {
00231         if (*first == val)
00232             return first;
00233     }
00234     return last;
00235 }
00236 
00237 /// calculates the mean and variance of a set of data, one element at a time
00238 template <typename T>
00239 class mean_variance
00240 {
00241     // s^2 = ( sum (x_i - x_1)^2 - ( sum (x_i - x_1) )^2 / n ) / (n-1)
00242 private:
00243     T mMean, mVar, mSum1, mSum2;
00244     const T mFirst;
00245     void operator=(const mean_variance&) { throw; }
00246 public:
00247     mean_variance(const T & first) : mFirst(first), mMean(0), mVar(0), mSum1(0), mSum2(0) {}
00248     void inline operator()(const T& elem)
00249     {
00250         T temp = elem - mFirst;
00251         mSum1 += temp*temp;
00252         mSum2 += temp;
00253     }
00254     T mean(long num_elems)
00255     {
00256         T temp = mSum2 / num_elems;
00257         temp += mFirst;
00258         return temp;
00259     }
00260     T var(long num_elems)
00261     {
00262         T temp = (mSum2*mSum2)/num_elems;
00263         temp = (mSum1 - temp)/(num_elems - 1);
00264         return temp;
00265     }
00266 };
00267 
00268 #endif
00269 

Generated on Mon Sep 12 19:58:27 2005 for Destiny3D by doxygen1.3-rc3